﻿using Lan.Dto;
using Lan.Infrastructure.CameraOnvif;
using Microsoft.AspNetCore.Mvc;
using Model;
using System.Text;

namespace Lan.Application.Controllers
{
    [Route("api/camera")]
    [ApiController]
    public class CameraController : BaseController
    {
        public readonly ICameraService _CameraService;
        public CameraController(ICameraService cameraService)
        {
            _CameraService = cameraService;
        }

        [HttpGet("list")]
        public IActionResult QueryRadar([FromQuery] CameraQueryDto parm)
        {
            var response = _CameraService.GetList(parm);
            return Message(response);
        }

        [HttpGet("{Id}")]
        public IActionResult GetCamera(int Id)
        {
            var response = _CameraService.GetInfo(Id);
            var info = response.Adapt<CameraDto>();
            return Message(info);
        }

        ONVIF_COMMON_INFO common = new ONVIF_COMMON_INFO();

        [HttpPost]
        public IActionResult AddCamera([FromBody] CameraDto parm)
        {
            ONVIF_MANAGEMENT_CAPABILITIES capabilities = new ONVIF_MANAGEMENT_CAPABILITIES();
            int ret = onvifsdk.ONVIF_MAGEMENT_GetCapabilitiesEx(20, parm.Ip, 80, parm.Username, parm.Password, ref capabilities);
            string strCameraURL = "";
            string strSourceToken = "";

            if (ret == 0)
            {
                common.username = parm.Username;
                common.password = parm.Password;
                common.onvifUrls = capabilities.onvifUrls;


                ONVIF_DEVICE_INFO aa = new ONVIF_DEVICE_INFO();
                ret = onvifsdk.ONVIF_MAGEMENT_GetDeviceInformation(3, ref common, ref aa);


                ONVIF_MEDIA_CHANNEL_SOURCE channel = new ONVIF_MEDIA_CHANNEL_SOURCE();
                ret = onvifsdk.ONVIF_MEDIA_GetChannelSource(3, ref common, ref channel);

                for (int ch = 0; ch < channel.sourceNum; ch++)
                {
                    strSourceToken = Encoding.Default.GetString(channel.sourceToken[ch].sourceToken);

                    byte[] rtspUrl = new byte[128];
                    ONVIF_MEDIA_STREAM_TYPE ONVIF_MEDIA_STREAM_TYPE;
                    ONVIF_MEDIA_STREAM_TYPE = ONVIF_MEDIA_STREAM_TYPE.ONVIF_MEDIA_TYPE_MAIN_STREAM;
                    ret = onvifsdk.ONVIF_MEDIA_GetStreamUri(3, ref common, strSourceToken, ONVIF_MEDIA_STREAM_TYPE, rtspUrl);
                    strCameraURL = Encoding.Default.GetString(rtspUrl);
                }
            }
            parm.CameraURL = strCameraURL;

            var modal = parm.Adapt<CameraModel>().ToCreate(HttpContext);

            var response = _CameraService.AddCamera(modal);

            return Message(response);
        }

        [HttpPut]
        public IActionResult UpdateCamera([FromBody] CameraDto parm)
        {
            var modal = parm.Adapt<CameraModel>().ToUpdate(HttpContext);
            var response = _CameraService.UpdateCamera(modal);

            return ToResponse(response);
        }

        [HttpDelete("delete/{ids}")]
        public IActionResult DeleteCamera([FromRoute] string ids)
        {
            var idArr = Lan.Tools.Tools.SplitAndConvert<int>(ids);

            return ToResponse(_CameraService.DeleteCamera(idArr));
        }


        //[HttpGet("dcamera/{DefenceAreaId}")]
        //public IActionResult GetCameraByDefenceAreaId(int DefenceAreaId)
        //{
        //    var response = _CameraService.GetCameraByDefenceAreaId(DefenceAreaId);
        //    var info = response.Adapt<CameraDto>();
        //    return Message(info);
        //}
    }
}
